package com.ijiuchuang.jc_localization.view.activity;

import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.CoordinatorLayout;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;

import com.ijiuchuang.jc_localization.R;
import com.ijiuchuang.jc_localization.base.BaseActivity;
import com.ijiuchuang.jc_localization.contract.tools.ToolsSignInContract;
import com.ijiuchuang.jc_localization.presenter.tools.ToolsSignInPresenter;

import java.lang.reflect.Field;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class ToolsSignInActivity extends BaseActivity implements ToolsSignInContract.View {

    @BindView(R.id.tools_sign_in_toolbar)
    Toolbar mToolbar;
    @BindView(R.id.tools_sign_in_fab)
    FloatingActionButton mFab;
    @BindView(R.id.tools_sign_in_coordinator)
    CoordinatorLayout mCoordinator;

    private ToolsSignInContract.Presenter presenter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_tools_sign_in);
        ButterKnife.bind(this);
        setSupportActionBar(mToolbar);
        mToolbar.setNavigationIcon(R.drawable.ic_arrow_back_white_24dp);
        mToolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        presenter = new ToolsSignInPresenter(this, this);
        presenter.start();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_tools_signin, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();

        switch (id) {
            case R.id.menu_tools_signin_account:
                return true;
            case R.id.menu_tools_signin_lookup:
                return true;
        }

        return super.onOptionsItemSelected(item);
    }

    @OnClick(R.id.tools_sign_in_fab)
    public void onClick() {
        Snackbar.make(mCoordinator, "Replace with your own action", Snackbar.LENGTH_LONG)
                .setAction("Action", null).show();
    }

    @Override
    public void setPresenter(ToolsSignInContract.Presenter presenter) {
        this.presenter = presenter;
    }

    @Override
    public void showProgressDialog(ProgressDialog dialog) {
        DialogInterface.OnKeyListener keylistener = new DialogInterface.OnKeyListener() {
            public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) {
                if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
                    finish();
                    return true;
                } else {
                    return false;
                }
            }
        };

        dialog.setMessage("检查用户...");
        dialog.setCanceledOnTouchOutside(false);// 设置点击屏幕Dialog不消失
        dialog.setOnKeyListener(keylistener);
        dialog.show();
    }

    @Override
    public void dismissProgressDialog(ProgressDialog dialog) {
        dialog.dismiss();
    }

    @Override
    public void showSetStudentDialog() {
        View view = View.inflate(this, R.layout.dialog_bind_student, null);
        final EditText editText = (EditText) view.findViewById(R.id.dialog_bind_student_edit_num);

        AlertDialog dialog = new AlertDialog.Builder(ToolsSignInActivity.this)
                .setTitle(R.string.dialog_title_info)
                .setView(view)
                .setPositiveButton(R.string.dialog_btn_submit, null)
                .setNegativeButton(R.string.dialog_btn_cancel, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                        finish();
                    }
                })
                .create();
        dialog.show();
    }
}
